package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.exceptor.BusinessException;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.remote.rmi._RMIConnection_Stub;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    /**
     * 根据环信id查询用户详情
     * */
    public UserInfoVo findUserInfoByHuanxin(String userId){
        //查询查询用户详情
        UserInfo userinfo = userInfoApi.findById(Long.valueOf(userId));

        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userinfo,vo);

        if (userinfo.getAge() != null){
            vo.setAge(userinfo.getAge().toString());
        }
        return vo;
    }


    /**
     * 添加好友
     * */
    public void contacts(Long friendId) {
        // 将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(), Constants.HX_USER_PREFIX + friendId);
     //   Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + 106L, Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }

        // 如果注册成功 记录好友关系到mongoDd
        friendApi.save(UserHolder.getUserId(),friendId);
     //   friendApi.save(106L,friendId);

    }

    /**
     * 分页查询联系人列表
     * */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        // 查询当前用户的好友数据
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        // 提取好友id列表
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);


        // 查询好友用户详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long, UserInfo>  map = userInfoApi.findByIds(userIds, info);

        // 构造vo 对象
        ArrayList<ContactVo> contactVos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if (userInfo != null){
                ContactVo vo = ContactVo.init(userInfo);
                contactVos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0L,contactVos);
    }


    // 接触环信的好友关系 删除数据库好友数据
    public void removeContacts(Long likeUserId) {
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(), Constants.HX_USER_PREFIX + likeUserId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }

        friendApi.delete(UserHolder.getUserId(),likeUserId);
    }
}
